/**
 *  Copyright (C) 2013  Piotr Szczepański
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package edu.pw.elka.gtsna.graph_evaluators;

import java.util.Set;

import edu.pw.elka.gtsna.community_detection.CommunityStructure;
import edu.pw.elka.gtsna.graph.Graph;

/**
 * @author Piotr Lech Szczepański
 * @author P.Szczepanski@ii.pw.edu.pl 
 *
 */
public class Modularity extends GraphPartitionEvaluator {

	
	/**
	 * @param graph
	 */
		public Modularity(Graph graph, CommunityStructure communities) {
			super(graph, communities);
		}
		
		public Modularity(Graph graph) {
			super(graph);
		}
	
		
	/* (non-Javadoc)
	 * @see edu.pw.elka.gtsna.graph_evaluators.GraphEvaluator#evaluate()
	 */
	@Override
	public double evaluate() {
		double modularity = 0.0;
		
		int l2 = 2*graph.getNumOfEdges();	
		
		for (Set<Integer> community: communities.getCommunities()){
//			double e = 0.0;
//			double a = 0.0;
			for (int node1: community){
//				e += graph.getIntraCommunityDegree(node, communities);	
//				a += graph.getDegree(node);
				for (int node2: community){
                    double delta = (graph.areNeighbours(node1,node2)?1:0) - (double)graph.getDegree(node1)*(double)graph.getDegree(node2)/l2;
                    // System.out.println("delta for vertex pair " + v1 + " , " + v2 + " is " + delta);
                    modularity += delta;  
				}
			}
//			modularity += (e/l2 - (a*a)/(l2*l2));				
		}
		
		return modularity/l2;
	}
	
	public double evaluate2() {
		double modularity = 0.0;
		
		int l2 = 2*graph.getNumOfEdges();	
		
//		for (Set<Integer> community: communities.getCommunities()){
//			double e = 0.0;
//			double a = 0.0;
			for (int node1: graph.getAllNodes()){
//				e += graph.getIntraCommunityDegree(node, communities);	
//				a += graph.getDegree(node);
				for (int node2: graph.getAllNodes()){
					if (communities.getNodeCommunityNumber(node1) == communities.getNodeCommunityNumber(node2) ) {
                    double delta = (graph.areNeighbours(node1,node2)?1:0) - (double)graph.getDegree(node1)*(double)graph.getDegree(node2)/l2;
                    // System.out.println("delta for vertex pair " + v1 + " , " + v2 + " is " + delta);
                    modularity += delta;  
					}
				}
			}
//			modularity += (e/l2 - (a*a)/(l2*l2));				
//		}
		
		return modularity/l2;
	}
	

}
